
package Menu;

import graphicswithsomebackup.MouseLocator;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 *
 * @author Martin Wilhelmsson
 */
public class Button {

    // --------- Primitive types --------------
    private int posX, posY, width, height, mouseX, mouseY, buttonMiddleX, buttonMiddleY;
    boolean isRect;
    boolean isClicked;
    //.........................................

    // --------- Reference types --------------
    private Image buttonImage;
    private JPanel GamePanel; //ett object som tar GamePanel-panelen.
    private MouseLocator mouseLocator;
    private Point mousePos = new Point();
    private String buttonImageName; //the name of the buttonimage + typ, like "start.gif". inserted when a button-object is created.
    //.........................................

    /**
     * The constructor. Note the mouseLocator-object, which is used to locate the mousePosition.
     * @param imageAdress - The image has to be placed in the menu submap
     * @param posX
     * @param posY
     * @param width
     * @param height
     * @param superPanel
     */
    public Button(String imageAdress, boolean isRect, int posX, int posY, int width, int height, JPanel superPanel, MouseLocator mouseLocator) {

        GamePanel = superPanel;
        buttonImageName = imageAdress; //enter the filename plus type.
        this.mouseLocator = mouseLocator;
        this.isRect = isRect;
        this.posX = posX;
        this.posY = posY;
        this.width = width;
        this.height = height;
        this.buttonMiddleX = posX+ (width/2);
        this.buttonMiddleY = posY+ (height/2);

        buttonImage = new ImageIcon("images/menu/"+buttonImageName).getImage(); //has to be placed in menu-map!

    }


    /**
     * Used in Menu painComponent when an object of a button is drawn.
     * @param g
     */
    public void drawButton(Graphics g, MouseLocator mouseLocator) {
        this.mouseLocator = mouseLocator;
        if(isRect){
            drawRectButton(g);
        }
        else if(!isRect) { //otherwise its a round button
            drawRoundButton(g);
        }
    }

    public void drawRectButton(Graphics g) {
        g.drawImage(buttonImage, posX, posY, width, height, GamePanel);
        checkIfRectangularIsPressed();
    }

    public void drawRoundButton(Graphics g) {
        g.drawImage(buttonImage, posX, posY, width, height, GamePanel);
        checkIfRoundIsPressed();

    }


    /**
     * Checks if the mousecoursor is inside the buttons borders, for a rectangular button
     * @return
     */
    private void checkIfRectangularIsPressed() {
        //The mouselocator is updated when a click is made is the Menu-class.
        mouseX = mouseLocator.getMouseX();
        mouseY = mouseLocator.getMouseY();

        //this if-statement checks if the cursor is within the borders.
        if(  (mouseX>=posX&&(mouseX<=posX+width) ) && (mouseY>=posY&&(mouseY<=posY+height) ) ) {
            isClicked = true;
        }
    }


    /**
     * Checks if mousecursor is over the round button! The button must be [perfecly round]<<<------!!!!
     * @return
     */
    private void checkIfRoundIsPressed() {

        double deltaX = (buttonMiddleX - mouseLocator.getMouseX() );
        double deltaY = (buttonMiddleY - mouseLocator.getMouseY() );
        //Pyth-theorem.
        double hypothenusa = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));

        if (hypothenusa <= (width/2) ) { //checks if the provided coordinates from mouse is inside
            isClicked = true; //then its clicked
        }
    }

    /**
     * Returns true if button is clicked. Used in "rectIsPressed" or "checkIfRoundIsPressed".
     * @return
     */
    public boolean isClicked() {
        return isClicked;
    }



    public void setXandY(int playerPosX, int playerPosY) {
        posX = playerPosX;
        posY = playerPosY;
    }

}